package com.hrt.common.systemmanager.systemsetting.dict.data;

import com.btcode.common.StringUtil;
import com.btcode.db.result.Row;
import com.btcode.db.sqlparameter.SQLParameterList;
import com.hrt.common.db.CommonDBManager;
import com.hrt.common.db.TreeDataUtil;
import com.hrt.common.web.BaseService;
import java.util.HashMap;
import java.util.List;

public class DictDataService extends BaseService {

    public List<Row> getList(String dictTypeCode) {
        String sql = "SELECT *\n" + "  FROM C_BC_DICT_DATA T\n"
            + " WHERE T.DICT_TYPE_CODE = @typeCode  order by PARENT_ID, CODE asc\n";
        SQLParameterList pc = new SQLParameterList();
        pc.put("typeCode", dictTypeCode);
        List<Row> rows = CommonDBManager.getDataExecuter().queryList(sql, pc);
        List<Row> result = TreeDataUtil.buildTrees(rows);
        return result;
    }

    public List<Row> getDictChildList(HashMap<String, String> params) {
        String dictTypeCode = params.get("DICT_TYPE_CODE");
        String parentCode = params.get("PARENT_CODE");
        SQLParameterList pc = new SQLParameterList();
        pc.put("dictTypeCode", dictTypeCode);
        String sql = "SELECT T.*\n" + "  FROM C_BC_DICT_DATA T\n"
            + " WHERE T.DICT_TYPE_CODE = @dictTypeCode\n";
        if (!StringUtil.isEmpty(parentCode)) {
            pc.put("code", parentCode);
            sql += "   and t.parent_id in (select t2.id\n"
                + "                         from C_BC_DICT_DATA t2\n"
                + "                        where t2.DICT_TYPE_CODE = @dictTypeCode and t2.code = @code)\n";
        }
        else {
            sql += "   and t.parent_id is null";
        }
        sql += " order by PARENT_ID, CODE asc";
        List<Row> rows = CommonDBManager.getDataExecuter().queryList(sql, pc);
        return rows;
    }

}
